<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ !currentID ? '添加学生履历' : '修改学生履历' }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form :form="form">
					<a-form-item label="学号">
						<a-select
							v-decorator="['UserNo', { rules: [{ required: true, message: '请选择学生' }] }]"
							showSearch
							optionFilterProp="children"
							style="width: 100%"
							:showArrow="false"
							@search="fetchUser"
							:filterOption="filterOption"
							placeholder="请输入关键字进行搜索"
							allowClear
						>
							<a-select-option v-for="(item, index) in userList" :key="item.UserNo">{{ item.UserNo }}&nbsp;|&nbsp;{{ item.Name }}</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="学期">
						<a-select :allowClear="true" v-decorator="['SemesterID',{ rules: [{ required: true, message: '请选择学期' }] }]" placeholder="全部"
							optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in SemesterList" :key="index" :value="item.ID">
								{{ item.Name }}
							</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="职务">
						<a-select :allowClear="true" v-decorator="['PostID', { rules: [{ required: true, message: '请选择职务' }] }]" placeholder="全部" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in PostList" :key="index" :value="item.ID">{{ item.Name }}</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="时间">
						<a-range-picker show-time format="YYYY-MM-DD HH:mm" v-decorator="['Data', { rules: [{ required: true, message: '请选择时间' }] }]" />
					</a-form-item>
					<a-form-item class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">{{ !currentID ? '确认添加' : '确认修改' }}</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-item>
				</a-form>
			</div> 
		</div>
	</div>
</template>

<script>
import moment from 'moment';
import { AddUserPost, UpdateUserPost,GetUserList,DoGetSemesterListCommon } from '@/api/follow';
export default {
	props: {
		text: Object,
		PostList:Array
	},
	watch: {
		text: function(text) {
			if (JSON.stringify(text) == '{}') {
				this.currentID = ''
			} else {
				let that = this;
				that.currentID = text.ID;
				that.userList = [{Name:text.UserName,UserNo: text.UserNo}]
				setTimeout(function() {
					that.form.setFieldsValue({
						UserNo: text.UserNo,
						PostID: text.PostID,
						SemesterID:text.SemesterID
					});
				}, 300);
				if(text.StartTime!=''&&text.EndTime!=''){
					setTimeout(function() {
						that.form.setFieldsValue({
							Data: [moment(text.StartTime), moment(text.EndTime)],
						});
					}, 300);
				}
			}
		}
	},
	data() {
		return {
			loading: false,
			formLayout: 'horizontal',
			form: this.$form.createForm(this, {
				name: 'coordinated'
			}),
			currentID: '',
			userList:[],
			SemesterList:[]
		};
	},
	created() {
		this.getOtherList()
	},
	methods: {
		moment,
		filterOption(input, option) {
			return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
		},
		async getOtherList() {
			let res= await DoGetSemesterListCommon();
			this.SemesterList = res.data.data;
		},
		async fetchUser(value) {
			if (value && value.length > 1) {
				let data = {};
				data.key = value;
				data.top = 20;
				let res = await GetUserList(data);
				this.userList = res.data.data;
			}
		},
		closeFunction(data) {
			this.form.resetFields();
			this.$emit('closeFun', data);
		},
		handleSubmit(e) {
			this.form.validateFields(async (err, values) => {
				if (!err) {
					let data = {};
					data = values
					data.StartTime = moment(values.Data[0]).format('YYYY-MM-DD HH:mm');
					data.EndTime = moment(values.Data[1]).format('YYYY-MM-DD HH:mm');
					let res = {};
					if (!this.currentID) {
						res = await AddUserPost(data);
					} else {
						data.id = this.currentID;
						res = await UpdateUserPost(data);
					}
					if (res.data.code === 0) {
						this.$message.success(res.data.msg);
						this.closeFunction('1');
					} else {
						this.$message.error(res.data.msg);
					}
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
/deep/.ant-form {
	margin-top: 50px;
	padding-bottom: 50px;
}
/deep/.ant-form-item {
	margin: 20px 50px;
	display: flex;
}
/deep/.ant-form-item-label {
	width: 100px;
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	text-align: right;
}
/deep/.ant-form-item-control-wrapper {
	flex: 1;
}
/deep/.ant-calendar-picker {
	width: 100%!important;
}
</style>
